import time

from django.http import HttpResponse
from django.shortcuts import render

import requests

from PIL import Image
import urllib.request
import os

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By

url = "https://tyutgs.wjx.cn/user/loginForm.aspx?user_token="


# 进行登录的css

def login(username, password):
    try:
        token = "C986fzY4Y8ANeie91V0XkpP4XeBHzWYe%2fA64fzr7fkPiRZf2kIaSoofLYyFRUMEBCGxP3CW6GDLy%2fA3R4%2fHM5lqgV6aXXLFDBvloBYX%2ffRxlBJtMmtTdTA%3d%3d"
        # 连接token
        url_token = url + token
        browser = webdriver.Chrome()
        # 访问页面
        browser.get(url_token)
        browser.find_element(By.ID, "register-user-name").clear()
        browser.find_element(By.ID, "register-user-name").send_keys(username)
        browser.find_element(By.ID, "register-user-password").clear()
        browser.find_element(By.ID, "register-user-password").send_keys(password)
        browser.find_element(By.ID, "btnSubmit").click()
        current_url = browser.current_url
        print("当前页面" + current_url)
        # 点击页面
        browser.find_element(By.XPATH, "//dl[@class='surveyItem'][1]/a").click()

        # 开始打卡
        browser.find_element(By.XPATH, "//label[@for='q1_1']").click()
        # 第二题
        browser.find_element(By.XPATH, "//label[@for='q2_1']").click()

        # time.sleep(3)
        try:
            # 第四题
            print("到这一步了")
            browser.find_element(By.XPATH, "//textarea[@id='q5']").click()
            #
            # browser.find_element(By.XPATH, "//div[@id='PDF_c_chezchenz']/div/a").click()
            time.sleep(10)
            browser.find_element(By.XPATH, "//button[@class='mainBgColor']").click()
            js = "document.getElementsByClassName('res_local')[0].innerHTML='山西省晋中市榆次区乌金山镇魏榆路209号太原理工大学明向校区[112.720752,37.750125]';" \
                 "document.getElementsByClassName('res_local')[0].style.display='block'"
            browser.execute_script(js)
            print("js变化过后的" + browser.find_element(By.XPATH, "//div[@class='res_local']").text)
        except Exception:
            print("确定地图的时候发生了错误")

        browser.find_element(By.XPATH, "//label[@for='q6_3']").click()
        time.sleep(1)
        browser.find_element(By.XPATH, "//label[@for='q7_3']").click()
        time.sleep(1)
        browser.find_element(By.XPATH, "//label[@for='q8_1']").click()
        time.sleep(1)
        browser.find_element(By.XPATH, "//a[@rel='q9_1']/following-sibling::label").click()
        time.sleep(1)
        browser.find_element(By.XPATH, "//a[@rel='q14_2']/following-sibling::label").click()
        time.sleep(1)
        browser.find_element(By.XPATH, "//textarea[@id='q16']").send_keys("15651771520")
        time.sleep(1)
        browser.find_element(By.XPATH, "//a[@rel='q17_1']/following-sibling::label").click()
        browser.find_element(By.ID, "submit_button").click()

        browser.find_element(By.XPATH, "//div[@class='rectMask']").click()
    except Exception:
        print("发送了错误", Exception)


pass


def hello(request):
    login("2021510494", "133415")
    return HttpResponse("Hello world ! ")
